package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.LinkBack;
import com.eduhzy.ttw.commonsdk.entity.PlatformNoticeData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerVerifyMessageComponent;
import com.eduhzy.ttw.teacher.mvp.contract.VerifyMessageContract;
import com.eduhzy.ttw.teacher.mvp.presenter.VerifyMessagePresenter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-7-20 17:16:22
 */
@Route(path = RouterHub.APP_VERIFYMESSAGEACTIVITY)
public class VerifyMessageActivity extends BaseActivity<VerifyMessagePresenter> implements VerifyMessageContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @Inject
    BaseQuickAdapter<PlatformNoticeData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    Gson mGson;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        setTitle("验证消息");
        //如找不到该类,请编译一下项目
        DaggerVerifyMessageComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_verify_message;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            PlatformNoticeData data = mAdapter.getData().get(position);
            if (ObjectUtils.isNotEmpty(data.getLinkBack())) {
                if (data.getDealStatus() == 1) {
                    return;
                }
                List<LinkBack> lists = mGson.fromJson(data.getLinkBack(), new TypeToken<List<LinkBack>>() {
                }.getType());
                LinkBack linkBack = lists.get(0);
                LinkBack.ParamsBean params = linkBack.getParams();
                mPresenter.joinedClassByManager(String.valueOf(params.getId()), SPUtils.getInstance().getString(Constants.USERID), String.valueOf(params.getAccessUserId()), data.getInboxId(), position);
            }
        });
        mAdapter.setOnItemLongClickListener((adapter, view, position) -> {
            PlatformNoticeData data = mAdapter.getData().get(position);
            RxUtil.showDeleteDialog(getActivity(), (dialog, index) -> {
                switch (index) {
                    case 0:
                        dialog.dismiss();
                        break;
                    case 1:
                        mPresenter.delApplyClass(data.getInboxId(), position);
                        dialog.dismiss();
                        break;
                    default:

                        break;
                }
            });

            return false;
        });
        mSwipeLayout.setOnRefreshListener(() -> {
            getData(true);
        });
        getData(true);
        mAdapter.setOnLoadMoreListener(() -> getData(false), rvList);
    }

    private void getData(boolean pullToRefresh) {
        mPresenter.getSendMsgList(pullToRefresh, 400);
    }

    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
